﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForumServer;
using ForumServer.Forum;
using log4net;
using ForumLibrary;
using ForumLibrary.Data;

namespace ForumGUI
{
    public partial class ProfileFrame : Form
    {
        private ForumManager _manager;
        private ClientController _clientController;
        private string _myUserString;
        private ILog _logger;

        public ProfileFrame(ref ForumManager manager, ref ClientController clientController, string myUserString)
        {
            _manager = manager;
            _clientController = clientController;
            _myUserString = myUserString;
            _logger = _clientController.Logger;
            InitializeComponent();
            _clientController.pushForm(this);
            this.refreshFriendsList();
            this.initUserDetails();
            this.refresNotification();
        }

        public void refresNotification()
        {
            notificationListBox.Items.Clear();
            this.notificationListBox.Items.AddRange(_clientController.getLastNotification());
        }

        private void initUserDetails()
        {
            string toParse = "";
            try
            {
                toParse = (string)_manager.getUser(this._myUserString);
                if (toParse.Contains("User details were sent"))
                {
                    _logger.Info(_myUserString + " got " + _myUserString + " details");

                }
                else
                    _logger.Error(_myUserString + " was unable to get " + _myUserString + " details");
            }
            catch
            {
                _logger.Error(_myUserString + " failed to get " + _myUserString + " details");
            }
            toParse = toParse.Substring(toParse.IndexOf('\n') + 1);
            string prof = this._myUserString+ " profile info: " + "\n";
            string fName = "first name: " + toParse.Substring(0, toParse.IndexOf('\n')) + "\n";
            toParse = toParse.Substring(toParse.IndexOf('\n') + 1);
            string lName = "last name: " + toParse.Substring(0, toParse.IndexOf('\n')) + "\n";
            toParse = toParse.Substring(toParse.IndexOf('\n') + 1);
            string email = "email: " + toParse.Substring(0, toParse.IndexOf('\n')) + "\n";
            toParse = toParse.Substring(toParse.IndexOf('\n') + 1);
            this.profileInfoLabel.Text = prof +fName + lName + email;

        }

        private void addNewFriend_Click(object sender, EventArgs e)
        {
            AddNewFriendFrame addNewFriendFrame = new AddNewFriendFrame( ref _manager,ref _clientController,_myUserString,ref _logger);
            _clientController.pushForm(this);
            this.Visible = false;
            addNewFriendFrame.Visible = true;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Form lastForm = _clientController.popForm();
            if (lastForm != null)
            {
                this.Dispose(false);
                lastForm.Visible = true;
            }
        }

        public void refreshFriendsList()
        {
            this.friendsList.Items.Clear();
            string toParse = "";
            try
            {
                toParse = (string)_manager.getFriends(_myUserString);
                if (toParse != null)
                    _logger.Info(_myUserString + " got it's friends list");
                else
                    _logger.Error(_myUserString + " failed to got it's friends list");
            }
            catch
            {
                _logger.Error(_myUserString + " failed to got it's friends list");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }

            if (!toParse.Contains("User has no friends!"))
            {
                String[] parse = toParse.Split('\n');
                foreach (String s in parse)
                {
                    if (!String.IsNullOrEmpty(s))
                        friendsList.Items.Add(s);
                }
            }
        }

        private void removeFriendButton_Click(object sender, EventArgs e)
        {
            string currSelected = (string)this.friendsList.SelectedItem;
            if (currSelected == null)
                return;

            try
            {
                DialogResult res = MessageBox.Show("Are you sure you want to remove "+currSelected +" from your friends?", "remove "+currSelected, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == System.Windows.Forms.DialogResult.Yes)
                {
                    if (_manager.removeFriend(_myUserString, currSelected).Contains("Friend removed successfully"))
                    {
                        _logger.Info(_myUserString + " removed " + currSelected + " from his friends");
                        refreshFriendsList();
                        MessageBox.Show(currSelected + " removed successfully");
                    }
                    else
                        _logger.Error(_myUserString + " was unable to remove " + currSelected + " from his friends");
                }
            }
            catch
            {
                _logger.Error(_myUserString + " failed to remove " + currSelected + " from his friends");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            this.refreshFriendsList();
            this.refresNotification();
        }

        private void friendsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            string currSelected = (string)this.friendsList.SelectedItem;
            if (currSelected == null)
                return;
            string toParse = "";
            try
            {
                toParse = (string)_manager.getUser(currSelected);
                if (toParse.Contains("User details were sent"))
                {
                    _logger.Info(_myUserString + " got " + currSelected + " details");

                }
                else
                    _logger.Error(_myUserString + " was unable to get " + currSelected + " details");
            }
            catch
            {
                _logger.Error(_myUserString + " failed to get " + currSelected + " details");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
            toParse = toParse.Substring(toParse.IndexOf('\n')+1);
            string fName = "first name: "+toParse.Substring(0,toParse.IndexOf('\n')) + "\n";
            toParse = toParse.Substring(toParse.IndexOf('\n') + 1);
            string lName = "last name: " + toParse.Substring(0, toParse.IndexOf('\n')) + "\n";
            toParse = toParse.Substring(toParse.IndexOf('\n') + 1);
            string email = "email: " + toParse.Substring(0, toParse.IndexOf('\n')) + "\n";
            toParse = toParse.Substring(toParse.IndexOf('\n') + 1);
            string connected = toParse.Substring(0, toParse.IndexOf('\n'));
            if (connected.ToLower().Contains("true".ToLower()))
                this.connectedFlag.BackColor = System.Drawing.Color.LawnGreen;
            else
                this.connectedFlag.BackColor = System.Drawing.Color.Red;
            this.connectedFlag.BackStyle = Microsoft.VisualBasic.PowerPacks.BackStyle.Opaque;
            this.infoLabelDetails.Text = fName+lName+email;


        }

    }
}
